package com.wica.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.tracing.dtrace.Attributes;
import com.wica.bean.Sellgood;
import com.wica.bean.Type;
import com.wica.bean.User;
import com.wica.mapper.BiddMapper;

@Controller
@RequestMapping("/")
public class BiddAction {
	
	@Autowired
	private BiddMapper mapper;
	public void setMapper(BiddMapper mapper) {
		this.mapper = mapper;
	}
	private Integer uuid = null;
	
	
	@RequestMapping("check")
	public @ResponseBody Map<String,Object> check(@RequestParam("name") String name){
		Map<String,Object> map = new HashMap<String, Object>();
		User user = mapper.login(name);
		
		if(user  ==null) {
			map.put("sta", 1);
		}else{
			map.put("sta", 0);
		}
		
		return map;
	}
	
	@RequestMapping("login")
	public String login(@Validated User user,Model model,HttpSession httpSession) {
		User user1 = mapper.login(user.getuName());
		if(user.getuPwd().equals(user1.getuPwd())) {
			httpSession.setAttribute("uid", user1.getUid());
			uuid = user1.getUid();
			return "goodlist";
		}
		return "index";
	}
	
	@RequestMapping("showType")
	public @ResponseBody Map<String,List<Type>> showType(){
		Map<String,List<Type>> map = new HashMap<String,List<Type>>();
		List<Type> list = mapper.selType();
		map.put("types", list);
		return map;
	}
	
	@RequestMapping("selAllSell")
	public String selAllSell(Model model){
		List<Sellgood> list = mapper.selAllSell();
		
		model.addAttribute("list", list);
		return "list";
	}
	
	@RequestMapping("addSell")
	public String addSell(@Validated Sellgood sellgood,Model model,HttpSession httpSession) {
		Integer uid = (Integer) httpSession.getAttribute("uid");
		User user = new User();
		user.setUid(uid);
		sellgood.setUid(user);
		mapper.addSell(sellgood);
		Sellgood sellGood = mapper.selSell(sellgood.getsName());
		model.addAttribute("sellGood", sellGood);
		return "jingjia";
	}
	
	@RequestMapping("updateSell")
	public String updateSell(@Validated Sellgood sellgood,Model model) {
		System.out.println(sellgood);
		mapper.updateSell(sellgood);
		return "redirect:/selAllSell";
	}
	
	@RequestMapping("update")
	public @ResponseBody Map<String,Object> update(@RequestParam("sid") Integer sid,@RequestParam("price") Integer price) {
		Map<String,Object> map = new HashMap<String,Object>();
		Sellgood sell = mapper.selSellByid(sid);
		
		String sftot = sell.getsThprice();
		String uidss = sell.getSftot();
		
		String[] price1 = sftot.split(",");
		String[] prices = sftot.split(",");
		String[] uids = uidss.split(",");
		
		StringBuffer sb = new StringBuffer();
		StringBuffer sb1 = new StringBuffer();
			
		if(Double.parseDouble(price1[0]) < price) {
			sb.append(price+",");
			sb.append(prices[0]+",");
			sb.append(prices[1]);
			
			sb1.append(uuid+",");
			sb1.append(uids[0]+",");
			sb1.append(uids[1]);
		}
		if(Double.parseDouble(price1[1]) < price && Double.parseDouble(price1[0]) > price) {
			sb.append(prices[0]+",");
			sb.append(price+",");
			sb.append(prices[1]);
			
			sb1.append(uids[0]+",");
			sb1.append(uuid+",");
			sb1.append(uids[1]);
		}
		if(Double.parseDouble(price1[2]) < price && Double.parseDouble(price1[1]) > price) {
			sb.append(prices[0]+",");
			sb.append(prices[1]+",");
			sb.append(price);
			
			sb1.append(uids[0]+",");
			sb1.append(uids[1]+",");
			sb1.append(uuid);
		}
		Sellgood sell1 = new Sellgood();
		sell1.setSftot(sb1.toString());
		sell1.setsThprice(sb.toString());
		sell1.setSid(sid);
		mapper.updatePrice(sell1);
		map.put("sta", 1);
		return map;
	}
	
}
